package com.ztgf.logistics.service.deppon;

import com.baomidou.mybatisplus.mapper.EntityWrapper;
import com.ztgf.common.constant.Result;
import com.ztgf.common.utils.CustomizeUtil;
import com.ztgf.logistics.models.dto.deppon.LogisticsBillDTO;
import com.ztgf.logistics.models.service.deppon.ILogisticsBillService;
import com.ztgf.logistics.models.service.deppon.ILogisticsOrderService;
import com.ztgf.logistics.models.service.deppon.ILogisticsUserService;
import com.ztgf.logistics.models.vo.LogisticsBillOrderDepponSnapshotExpotVO;
import com.ztgf.logistics.models.vo.deppon.LogisticsOrderVO;
import org.apache.dubbo.config.annotation.DubboService;
import org.springframework.beans.BeanUtils;
import javax.annotation.Resource;
import com.ztgf.logistics.common.config.YmlConfig;
import com.baomidou.mybatisplus.service.impl.ServiceImpl;
import com.ztgf.logistics.mapper.deppon.LogisticsBillOrderDepponSnapshotMapper;
import com.ztgf.logistics.models.service.deppon.ILogisticsBillOrderDepponSnapshotService;
import com.ztgf.logistics.models.bo.deppon.LogisticsBillOrderDepponSnapshotSaveBO;
import com.ztgf.logistics.models.dto.deppon.LogisticsBillOrderDepponSnapshotDTO;

import java.math.BigDecimal;
import java.util.ArrayList;
import java.util.List;

/**
 * 物流德邦日志快照表控制器
 *
 * @author mxli
 * @Date 2021-09-13 22:14:38
 */
@DubboService
public class LogisticsBillOrderDepponSnapshotServiceImpl extends ServiceImpl<LogisticsBillOrderDepponSnapshotMapper, LogisticsBillOrderDepponSnapshotDTO> implements ILogisticsBillOrderDepponSnapshotService{

    @Resource
    private YmlConfig ymlConfig;
    @Resource
    private ILogisticsBillService logisticsBillService;
    @Resource
    private ILogisticsOrderService logisticsOrderService;
    @Override
    public boolean add(LogisticsBillOrderDepponSnapshotSaveBO saveBO) {
        LogisticsBillOrderDepponSnapshotDTO dto = new LogisticsBillOrderDepponSnapshotDTO();
        BeanUtils.copyProperties(saveBO,dto);
        Long id = ymlConfig.getPkId();
        dto.setId(id);
        return this.insert(dto);
    }

    @Override
    public List<LogisticsBillOrderDepponSnapshotExpotVO> queryExportList(String ids,Long billId) {
        List<LogisticsBillOrderDepponSnapshotExpotVO> list= new ArrayList<>();
        if (ids!=null){
            String[] split = ids.split(",");
            for (String id : split) {
                LogisticsBillOrderDepponSnapshotExpotVO logisticsBillOrderDepponSnapshotExpotVO = new LogisticsBillOrderDepponSnapshotExpotVO();
                LogisticsBillOrderDepponSnapshotDTO vo = this.selectById(id);
                    if (vo!=null){
                        LogisticsBillDTO logisticsBillDTO = logisticsBillService.selectById(vo.getBillId());
                        if (logisticsBillDTO!=null){
                            //支付状态
                            if (logisticsBillDTO.getPayStatus()==0){
                                logisticsBillOrderDepponSnapshotExpotVO.setPayStatus("未支付");
                            }
                            if (logisticsBillDTO.getPayStatus()==1){
                                logisticsBillOrderDepponSnapshotExpotVO.setPayStatus("支付中");
                            }
                            if (logisticsBillDTO.getPayStatus()==2){
                                logisticsBillOrderDepponSnapshotExpotVO.setPayStatus("支付成功");
                            }
                            if (logisticsBillDTO.getPayStatus()==3){
                                logisticsBillOrderDepponSnapshotExpotVO.setPayStatus("支付失败");
                            }

                        }
                        logisticsBillOrderDepponSnapshotExpotVO.setOrderTime(vo.getOrderTime());
                        logisticsBillOrderDepponSnapshotExpotVO.setMailNo(vo.getMailNo());
                        logisticsBillOrderDepponSnapshotExpotVO.setUserName(vo.getUserName());
//                        if (vo.getMemberType()==0){
//                            logisticsBillOrderDepponSnapshotExpotVO.setMemberType("普通会员");
//                        }
//                        if (vo.getMemberType()==1){
//                            logisticsBillOrderDepponSnapshotExpotVO.setMemberType("平台用户账单");
//                        }
//                        if(vo.getStatus()==0){
//                            logisticsBillOrderDepponSnapshotExpotVO.setStatus("正常");
//                        }
//                        if(vo.getStatus()==1){
//                            logisticsBillOrderDepponSnapshotExpotVO.setStatus("异常");
//                        }
                        //德邦金额
                        if (vo.getDepponMoney()!=null){
                            logisticsBillOrderDepponSnapshotExpotVO.setDepponMoney(vo.getDepponMoney().setScale(2,BigDecimal.ROUND_UP));
                        } else {
                            logisticsBillOrderDepponSnapshotExpotVO.setDepponMoney(new BigDecimal(0.00).setScale(2, BigDecimal.ROUND_UP));
                        }
//                      //平台金额
//                        if (vo.getMoney()!=null){
//                            logisticsBillOrderDepponSnapshotExpotVO.setMoney(vo.getMoney().setScale(2,BigDecimal.ROUND_UP));
//                        } else {
//                            logisticsBillOrderDepponSnapshotExpotVO.setMoney(new BigDecimal(0.00).setScale(2, BigDecimal.ROUND_UP));
//                        }
//                        logisticsBillOrderDepponSnapshotExpotVO.setRemark(vo.getRemark());
//                        //确认状态
//                        if (vo.getSureStatus()==0){
//                            logisticsBillOrderDepponSnapshotExpotVO.setSureStatus("未确认");
//                        }
//                        if (vo.getSureStatus()==1){
//                            logisticsBillOrderDepponSnapshotExpotVO.setSureStatus("已确认");
//                        }
                        Result<LogisticsOrderVO> logisticsOrderVOResult = logisticsOrderService.get(vo.getOrderId());
                        if (logisticsOrderVOResult.getData()!=null){
                            LogisticsOrderVO logisticsOrderVO=logisticsOrderVOResult.getData();
                            logisticsBillOrderDepponSnapshotExpotVO.setReceiverName(logisticsOrderVO.getReceiverName());
                            logisticsBillOrderDepponSnapshotExpotVO.setSenderName(logisticsOrderVO.getSenderName());
                            logisticsBillOrderDepponSnapshotExpotVO.setCargoName(logisticsOrderVO.getCargoName());
                            if (logisticsOrderVO.getTotalWeight() != null) {
                                logisticsBillOrderDepponSnapshotExpotVO.setTotalWeight(logisticsOrderVO.getTotalWeight().setScale(2, BigDecimal.ROUND_UP));
                            } else {
                                logisticsBillOrderDepponSnapshotExpotVO.setTotalWeight(new BigDecimal(0.00).setScale(2, BigDecimal.ROUND_UP));
                            }
                            logisticsBillOrderDepponSnapshotExpotVO.setTotalNumber(logisticsOrderVO.getTotalNumber());
                            logisticsBillOrderDepponSnapshotExpotVO.setDepartureStation(logisticsOrderVO.getSenderProvince() + "-" + logisticsOrderVO.getSenderCity() + "-" + logisticsOrderVO.getSenderCounty() + "-" + logisticsOrderVO.getSenderAddress());
                            logisticsBillOrderDepponSnapshotExpotVO.setDestination(logisticsOrderVO.getReceiverProvince() + "-" + logisticsOrderVO.getReceiverCity() + "-" + logisticsOrderVO.getReceiverCounty() + "-" + logisticsOrderVO.getReceiverAddress());
                            list.add(logisticsBillOrderDepponSnapshotExpotVO);
                        }
                    }
            }

        }else {
            //导出全部
            EntityWrapper<LogisticsBillOrderDepponSnapshotDTO> wrapper = new EntityWrapper<>();
            wrapper.eq("bill_id",billId);
            List<LogisticsBillOrderDepponSnapshotDTO> logisticsBillOrderDepponSnapshotDTOS = this.selectList(wrapper);
            if (logisticsBillOrderDepponSnapshotDTOS!=null){
                for (LogisticsBillOrderDepponSnapshotDTO vo : logisticsBillOrderDepponSnapshotDTOS) {
                    LogisticsBillOrderDepponSnapshotExpotVO logisticsBillOrderDepponSnapshotExpotVO = new LogisticsBillOrderDepponSnapshotExpotVO();
                    if (vo!=null){
                        LogisticsBillDTO logisticsBillDTO = logisticsBillService.selectById(vo.getBillId());
                        if (logisticsBillDTO!=null){
                            //支付状态
                            if (logisticsBillDTO.getPayStatus()==0){
                                logisticsBillOrderDepponSnapshotExpotVO.setPayStatus("未支付");
                            }
                            if (logisticsBillDTO.getPayStatus()==1){
                                logisticsBillOrderDepponSnapshotExpotVO.setPayStatus("支付中");
                            }
                            if (logisticsBillDTO.getPayStatus()==2){
                                logisticsBillOrderDepponSnapshotExpotVO.setPayStatus("支付成功");
                            }
                            if (logisticsBillDTO.getPayStatus()==3){
                                logisticsBillOrderDepponSnapshotExpotVO.setPayStatus("支付失败");
                            }

                        }
                        logisticsBillOrderDepponSnapshotExpotVO.setOrderTime(vo.getOrderTime());
                        logisticsBillOrderDepponSnapshotExpotVO.setMailNo(vo.getMailNo());
                        logisticsBillOrderDepponSnapshotExpotVO.setUserName(vo.getUserName());
//                        if (vo.getMemberType()==0){
//                            logisticsBillOrderDepponSnapshotExpotVO.setMemberType("普通会员");
//                        }
//                        if (vo.getMemberType()==1){
//                            logisticsBillOrderDepponSnapshotExpotVO.setMemberType("平台用户账单");
//                        }
//                        if(vo.getStatus()==0){
//                            logisticsBillOrderDepponSnapshotExpotVO.setStatus("正常");
//                        }
//                        if(vo.getStatus()==1){
//                            logisticsBillOrderDepponSnapshotExpotVO.setStatus("异常");
//                        }
                        //德邦金额
                        if (vo.getDepponMoney()!=null){
                            logisticsBillOrderDepponSnapshotExpotVO.setDepponMoney(vo.getDepponMoney().setScale(2,BigDecimal.ROUND_UP));
                        } else {
                            logisticsBillOrderDepponSnapshotExpotVO.setDepponMoney(new BigDecimal(0.00).setScale(2, BigDecimal.ROUND_UP));
                        }
//                        //平台金额
//                        if (vo.getMoney()!=null){
//                            logisticsBillOrderDepponSnapshotExpotVO.setMoney(vo.getMoney().setScale(2,BigDecimal.ROUND_UP));
//                        } else {
//                            logisticsBillOrderDepponSnapshotExpotVO.setMoney(new BigDecimal(0.00).setScale(2, BigDecimal.ROUND_UP));
//                        }
//                        logisticsBillOrderDepponSnapshotExpotVO.setRemark(vo.getRemark());
//                        //确认状态
//                        if (vo.getSureStatus()==0){
//                            logisticsBillOrderDepponSnapshotExpotVO.setSureStatus("未确认");
//                        }
//                        if (vo.getSureStatus()==1){
//                            logisticsBillOrderDepponSnapshotExpotVO.setSureStatus("已确认");
//                        }
                        Result<LogisticsOrderVO> logisticsOrderVOResult = logisticsOrderService.get(vo.getOrderId());
                        if (logisticsOrderVOResult.getData()!=null){
                            LogisticsOrderVO logisticsOrderVO=logisticsOrderVOResult.getData();
                            logisticsBillOrderDepponSnapshotExpotVO.setReceiverName(logisticsOrderVO.getReceiverName());
                            logisticsBillOrderDepponSnapshotExpotVO.setSenderName(logisticsOrderVO.getSenderName());
                            logisticsBillOrderDepponSnapshotExpotVO.setCargoName(logisticsOrderVO.getCargoName());
                            if (logisticsOrderVO.getTotalWeight() != null) {
                                logisticsBillOrderDepponSnapshotExpotVO.setTotalWeight(logisticsOrderVO.getTotalWeight().setScale(2, BigDecimal.ROUND_UP));
                            } else {
                                logisticsBillOrderDepponSnapshotExpotVO.setTotalWeight(new BigDecimal(0.00).setScale(2, BigDecimal.ROUND_UP));
                            }
                            logisticsBillOrderDepponSnapshotExpotVO.setTotalNumber(logisticsOrderVO.getTotalNumber());
                            logisticsBillOrderDepponSnapshotExpotVO.setDepartureStation(logisticsOrderVO.getSenderProvince() + "-" + logisticsOrderVO.getSenderCity() + "-" + logisticsOrderVO.getSenderCounty() + "-" + logisticsOrderVO.getSenderAddress());
                            logisticsBillOrderDepponSnapshotExpotVO.setDestination(logisticsOrderVO.getReceiverProvince() + "-" + logisticsOrderVO.getReceiverCity() + "-" + logisticsOrderVO.getReceiverCounty() + "-" + logisticsOrderVO.getReceiverAddress());
                            list.add(logisticsBillOrderDepponSnapshotExpotVO);
                        }
                    }
                }
            }
        }
        if (CustomizeUtil.listIsNull(list)){
            LogisticsBillOrderDepponSnapshotExpotVO logisticsBillOrderDepponSnapshotExpotVO = new LogisticsBillOrderDepponSnapshotExpotVO();
            list.add(logisticsBillOrderDepponSnapshotExpotVO);
        }
        return list;
    }
}
